package com.example.workmode;

import java.util.ArrayList;
import java.util.List;

import android.content.SharedPreferences;
import android.content.pm.ApplicationInfo;
import android.content.pm.PackageManager;
import android.os.AsyncTask;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.CheckedTextView;
import android.widget.ListView;

import com.example.workmode.AppManager.AppAdapter;


public class AppManagerFragment extends Fragment {
	private PackageManager packageManager = null;
	private List<ApplicationInfo> applist = null;
	private AppAdapter listadapter = null;
	private ListView listview;
	private String blockingApps = "";
	//private List<Boolean> listShow;
	private View view;
	
	@Override
	public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
		view = inflater.inflate(R.layout.fragment_app_manager, container, false);
		packageManager = view.getContext().getPackageManager();
		listview = (ListView) view.findViewById(R.id.listofapp);
		//listShow = new ArrayList<Boolean>();
        new LoadApplications().execute();
        listview.setOnItemClickListener(new OnItemClickListener() {

			@Override
			public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
				CheckedTextView chkItem = (CheckedTextView) view.findViewById(R.id.check);
                
               // Toast.makeText(view.getContext(), "Chosen no. "+(position+1), Toast.LENGTH_SHORT).show();
                
                if (chkItem.isChecked()) {
                	String app = applist.get((int) id).packageName + ";";
                	SharedPreferences pref = view.getContext().getSharedPreferences("MyPrefs", 0);
                	SharedPreferences.Editor editor = pref.edit(); 
                	blockingApps = pref.getString("blockingApps", null);
                	blockingApps = blockingApps.replace(app, "");
                	editor.putString("blockingApps", blockingApps);
    				editor.commit();
                } else {
                	String app = applist.get((int) id).packageName + ";";
                	blockingApps = blockingApps.concat(app);
                	SharedPreferences pref = view.getContext().getSharedPreferences("MyPrefs", 0);
    				SharedPreferences.Editor editor = pref.edit(); 
    				editor.putString("blockingApps", blockingApps);
    				editor.commit();
                }
               // Log.i("HERE", blockingApps);
                chkItem.setChecked(!chkItem.isChecked());
                //listShow.set((int)id, chkItem.isChecked());
			}
        });
        listview.setAdapter(listadapter);
        return view;
	}
	
    private List<ApplicationInfo> checkForLaunchIntent(List<ApplicationInfo> list) {
    	
    	ArrayList<ApplicationInfo> appList = new ArrayList<ApplicationInfo>();
    	
    	for(ApplicationInfo info : list) {
    		try{
    			if(packageManager.getLaunchIntentForPackage(info.packageName) != null) {
    				appList.add(info);
    			}
    		} catch(Exception e) {
    			e.printStackTrace();
    		}
    	}
    	
    	return appList;
    }
    
    private class LoadApplications extends AsyncTask<Void, Void, Void> {

    	//private ProgressDialog progress = null;
    	
		@Override
		protected Void doInBackground(Void... params) {
			
			applist = checkForLaunchIntent(packageManager.getInstalledApplications(PackageManager.GET_META_DATA));
			
			listadapter = new AppAdapter(view.getContext(), R.layout.app_row_layout, applist);
			
			return null;
		}
    	

    }
    
}

